package Control;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import Model.GioHang;
import Model.LineMobile;
import Model.Mobile;

/**
 * Servlet implementation class Order
 */
public class Order extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Order() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String action = request.getParameter("action");

		int mobileID = 0;
	        if (request.getParameter("mobileID") != null) {
	            mobileID = Integer.parseInt(request.getParameter("mobileID"));
	        }
	        String quantitystr = request.getParameter("quantity");
//	        String update = request.getParameter("update");
	        HttpSession session = request.getSession();
	        synchronized (session) {
	       //     Cart cart = (Cart) session.getAttribute("cart");
	        	GioHang cart=(GioHang)session.getAttribute("cart");
	            if (cart == null) {
	               // cart = new Cart();
	            	cart=new GioHang();
	                session.setAttribute("cart", cart);
	            }
	            int quantity = 1;
	            try {
	                quantity = Integer.parseInt(quantitystr);
	                if (quantity < 0) {
	                    quantity = 1;
	                }
	            } catch (NumberFormatException nfe) {
	                quantity = 1;
	            }

	            Mobile mb = new Mobile().getSanPham(mobileID);
	            //Khi khong cap nhat, add them 1
	            if (quantity>0) {
	            	mb.setSoLuong(quantity);
	            	if(action != null && action.equalsIgnoreCase("update")){
	            		cart.update(mb);
	            	}else{
	            		cart.add(mb);
	            	}
	            }
	            
	            if (quantity <= 0) {
	                cart.remove(mb);
	            }
	            session.setAttribute("cart", cart);
	            String url = "/cart.jsp";
	            RequestDispatcher dispathcer = getServletContext().getRequestDispatcher(url);
	            dispathcer.forward(request, response);
	        }
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
